package com.muqi.app.sharepreference;

import com.muqi.app.project.contants.MContants;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * 为保持登录token状态建立文件工具
 * 
 * @author John
 * 
 */
public class SharePreferenceUtil {
	private Context mcontext;
	private String fileName;

	private SharedPreferences sp;
	private SharedPreferences.Editor editor;

	@SuppressLint("CommitPrefEdits")
	public SharePreferenceUtil(Context context, String file) {
		mcontext = context;
		fileName = file;
		sp = context.getSharedPreferences(file, Context.MODE_PRIVATE);
		editor = sp.edit();
	}

	/**
	 * 
	 * @param token
	 */
	public void setToken(String token) {
		editor.putString("token", token);
		editor.commit();
	}

	public String getToken() {
		return sp.getString("token", "");
	}

	/**
	 * 登录类型
	 * 
	 * @param type
	 */
	public void setUserType(String type) {
		editor.putString("LoginType", type);
		editor.commit();
	}

	public String getUserType() {
		return sp.getString("LoginType", MContants.LOGIN_AS_STUDENT);
	}

	/**
	 * 
	 * @param userId
	 */
	public void setUserId(String userId) {
		editor.putString("userId", userId);
		editor.commit();
	}

	public String getUserId() {
		return sp.getString("userId", "");
	}

	/**
	 * 
	 * @param nickname
	 */
	public void setNickname(String nickname) {
		editor.putString("nickname", nickname);
		editor.commit();
	}

	public String getNickname() {
		return sp.getString("nickname", "");
	}

	/**
	 * 当前选中的childId
	 * 
	 * @param childId
	 */
	public void setCurrentChildId(String childId) {
		editor.putString("childId", childId);
		editor.commit();
	}

	public String getCurrentChildId() {
		return sp.getString("childId", "0");
	}

	/**
	 * 当前选中的孩子的classid
	 * 
	 * @param classId
	 */
	public void setChildClassId(String classId) {
		editor.putString("classId", classId);
		editor.commit();
	}

	public String getChildClassId() {
		return sp.getString("classId", "0");
	}

	public void setChildGradeId(String gradeId) {
		editor.putString("gradeId", gradeId);
		editor.commit();
	}

	public String getChildGradeId() {
		return sp.getString("gradeId", "0");
	}

	public void setChildSchoolId(String schoolId) {
		editor.putString("schoolId", schoolId);
		editor.commit();
	}

	public String getChildSchoolId() {
		return sp.getString("schoolId", "0");
	}

	public void setChildName(String childName) {
		editor.putString("childName", childName);
		editor.commit();
	}

	public String getCurrentChildName() {
		return sp.getString("childName", "");
	}
	
	public void setCurrentChildSex(String sex) {
		editor.putString("sex", sex);
		editor.commit();
	}
	
	public String getCurrentChildSex() {
		return sp.getString("sex", "");
	}
	
	/**
	 *  获得当前孩子的头像
	 */
	public String getCurrentAva(){
		return sp.getString("ava", "");
	}
	
	public void setCurrentAva(String ava) {
		editor.putString("ava", ava);
		editor.commit();
	}
	/**
	 *  获得当前孩子的imuser
	 */
	public String getCurrentIm(){
		return sp.getString("im", "");
	}
	
	public void setCurrentIm(String im) {
		editor.putString("im", im);
		editor.commit();
	}
	
	/********************************* Login Data **************************************/
	/**
	 * 
	 * @param username
	 */
	public String getUserName() {
		return sp.getString("username", "");
	}

	public void setUserName(String username) {
		editor.putString("username", username);
		editor.commit();
	}

	/**
	 * 
	 * @param password
	 */
	public void setPassword(String password) {
		editor.putString("password", password);
		editor.commit();
	}

	public String getPassword() {
		return sp.getString("password", "");
	}

	/**
	 *  设置对应通知类型的条数
	 * @param key （student_id+type）
	 * @param count
	 */
	public void setNotification(String key,int count){
		editor.putInt(key, count);
		editor.commit();
	}

	public int getNotification(String key){
		return sp.getInt(key, 0);
	}
	

	/********************************* shopping Car **************************************/
	/**
	 * 购物车数量
	 * 
	 * @param number
	 */
	public void setShoppingCarNum(int number) {
		editor.putInt("ShopCarNumber", number);
		editor.commit();
	}

	public int getShoppingCarNum() {
		return sp.getInt("ShopCarNumber", 0);
	}

	public void setClassName(String className) {
		editor.putString("className", className);
		editor.commit();
	}
	public void setSchoolName(String schoolName) {
		editor.putString("schoolName", schoolName);
		editor.commit();
	}
	public String getClassName() {
		return sp.getString("className", "");
	}
	
	public String getSchoolName() {
		return sp.getString("schoolName", "");
	}

	/**
	 * 清除SharedPreferences的内容
	 */
	public void clearSharedPreferences() {
		SharedPreferences sp = mcontext.getSharedPreferences(fileName,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}
}
